package com.scs.application.modules.device.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.modules.device.dto.PushSharePartDto;
import com.scs.application.modules.device.entity.DeviceCirculateSettings;
import com.scs.application.modules.device.mapper.DeviceCirculateSettingsMapper;
import com.scs.application.modules.device.service.DeviceCirculateSettingsService;
import com.scs.application.modules.device.service.DeviceLedgerService;
import com.scs.application.modules.sys.model.RestResult;
import com.scs.application.modules.thirdparty.service.MscmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 *  设备共享设置 服务实现类
 */
@Service
public class DeviceCirculateSettingsServiceImpl extends BaseServiceImpl<DeviceCirculateSettingsMapper, DeviceCirculateSettings> implements DeviceCirculateSettingsService {
    @Autowired
    private DeviceLedgerService deviceLedgerService;

    @Autowired
    private MscmService mscmService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateBatch(Collection<DeviceCirculateSettings> entityList) {
//        try {
//            deviceLedgerService.lockBatch(
//                    entityList.stream().map(DeviceCirculateSettings::getLedgerId).collect(Collectors.toList()),
//                    StrUtil.format("共享设置锁定，锁定日期: {}", DateUtils.nowDate(DateUtils.YYYYMMDDHHMMSS_PATTER2))
//            );
//        } catch (ParseException e) {
//            throw new RuntimeException(e);
//        }

        return super.saveOrUpdateBatch(entityList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeCascadeByIds(Collection<? extends Serializable> idList) {
//        Collection<DeviceCirculateSettings> settings =  this.listByIds(idList);
//        deviceLedgerService.unlockBatch(settings.stream().map(DeviceCirculateSettings::getLedgerId).collect(Collectors.toList()));
        return super.removeCascadeByIds(idList);
    }

    @Override
    public RestResult pushAll() {
        List<DeviceCirculateSettings> settingsList = this.list(Wrappers.<DeviceCirculateSettings>query().eq("set_type", "set_part_shart"));
        List<PushSharePartDto> pushList = new ArrayList<>();
        PushSharePartDto pushOne;
        if (ObjectUtil.length(settingsList) > 0) {
            for(DeviceCirculateSettings item : settingsList){
                pushOne = new PushSharePartDto();
                pushOne.setPartName(item.getDeviceName()).setPartSpec(item.getMatrSpec()).setPartSn(item.getDeviceSn())
                        .setStockId(item.getStockId()).setMfgName(item.getMfgName()).setSkuPrice(item.getSkuPrice())
                        .setSkuQty(item.getSkuQty()).setAdapterDeviceName(item.getAdapterDeviceName()).setAdapterDeviceSpec(item.getAdapterDeviceSpec());
                pushList.add(pushOne);
            }
        }
        mscmService.pushSharePart(pushList);
        return RestResult.ok("推送成功");
    }
}